// Created by WXX on 2021/11/20 8:35
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

/**
 * 执行用时：56 ms, 在所有 C++ 提交中击败了92.42%的用户
 * 内存消耗：31.7 MB, 在所有 C++ 提交中击败了88.00%的用户
 */
class Solution {
public:
    int findLHS(vector<int> &nums) {
        sort(nums.begin(), nums.end());
        int res = 0;
        for (int i = 0, j = 0; i < nums.size(); i++) {
            while (nums[i] - nums[j] > 1) j++;
            if (nums[i] - nums[j] == 1) res = max(res, i - j + 1);
        }
        return res;
    }
};

int main() {

    vector<int> nums = {1, 3, 2, 2, 5, 2, 3, 7};
    cout << Solution().findLHS(nums) << endl;  // 5

    return 0;
}
